package dk.dtu.compute.course02312.matadordel1;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.ImageView;
import android.widget.TextView;

public class BoardController {
	ImageView die1View;
	ImageView die2View;
	TextView messageArea;

	Context context;
	private Bitmap[] diceIcons = new Bitmap[6];

	/**
	 * Contains service methods for board for controlling the board.
	 */
	public BoardController() {
		this.context = MainActivity.context;
		this.die1View = (ImageView) ((Activity) MainActivity.context)
				.findViewById(R.id.die1);
		this.die2View = (ImageView) ((Activity) MainActivity.context)
				.findViewById(R.id.die2);
		this.messageArea = (TextView) ((Activity) MainActivity.context)
				.findViewById(R.id.message);
		makeDice();
	}

	/**
	 * Makes images of the dice
	 */
	private void makeDice() {
		Bitmap image = BitmapFactory.decodeResource(
				this.context.getResources(), R.drawable.dice);
		int height = (image.getHeight() + 1) / 6;
		for (int value = 0; value < 6; value++) {
			int x = 0;
			int y = height * value;
			this.diceIcons[value] = Bitmap.createBitmap(image, x, y,
					image.getWidth(), height - 1);
		}
	}

	public Bitmap getDiceIcon(int iconIndex) {
		if ((iconIndex >= this.diceIcons.length) || (iconIndex < 0))
			return null;
		return this.diceIcons[iconIndex];
	}

	public void setDice(int faceValue1, int faceValue2) {
		String[] dfvd = this.context.getResources().getStringArray(R.array.dice_face_values_description);
		this.die1View.setImageBitmap(this.getDiceIcon(faceValue1 - 1));
		this.die1View.setContentDescription(dfvd[faceValue1 - 1]);
		this.die2View.setImageBitmap(this.getDiceIcon(faceValue2 - 1));
		this.die2View.setContentDescription(dfvd[faceValue2 - 1]);
	}

	public void showMessage(String msg) {
		this.messageArea.setText(msg);
	}

}
